<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click="hAdd"
                >新增角色</el-button
              >
            </el-row>
            <!-- 表格 -->
            <el-table :data="roles" style="width: 100%">
              <!-- type:index会自动生成序号 -->
              <el-table-column label="序号" type="index" width="50" />
              <el-table-column prop="name" label="角色名称" width="180" />
              <el-table-column prop="description" label="描述" />
              <el-table-column width="300" label="操作">
                <template #default="{ row }">
                  <el-button size="mini" type="success" @click="hAssign(row.id)"
                    >分配权限</el-button
                  >
                  <el-button size="mini" @click="hEdit(row.id)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="hDel(row.id)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>

            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pageParams.page"
                :page-sizes="[2, 5, 10, 20]"
                :page-size="pageParams.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
              >
              </el-pagination>
              <!-- 弹层 -->
              <el-dialog
                :title="isEdit ? '编辑' : '添加'"
                :visible.sync="showDialog"
                @close="resetForm"
              >
                <el-form
                  ref="roleForm"
                  :model="roleForm"
                  :rules="rules"
                  label-width="100px"
                >
                  <el-form-item label="角色名称" prop="name">
                    <el-input v-model="roleForm.name" />
                  </el-form-item>
                  <el-form-item label="角色描述" prop="description">
                    <el-input v-model="roleForm.description" />
                  </el-form-item>
                </el-form>
                <!-- 底部 -->
                <el-row slot="footer" type="flex" justify="center">
                  <el-col :span="6">
                    <el-button size="small">取消</el-button>
                    <el-button size="small" type="primary" @click="hSubmit"
                      >确定</el-button
                    >
                  </el-col>
                </el-row>
              </el-dialog>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <el-dialog
        title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
        :visible.sync="showPerDialog"
      >
        <AssignPermission @close="showPerDialog = false" v-if="showPerDialog" :roleId='roleId'/>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import {
  getRoles,
  addRoles,
  delRoles,
  getRolesDetail,
  updateRoles,
} from "@/api/setting";
import AssignPermission from "./assignPermission.vue";
export default {
  components: {
    AssignPermission,
  },
  data() {
    return {
      roleId:'',
      activeName: "first",
      roles: [],
      pageParams: {
        page: 1, // 查询第一页
        pagesize: 2, // 每页两条  --- 要与pagination中page-size一致
      },
      total: 0, // 总的数据条数
      showDialog: false, // 关
      // 控制分配权限
      showPerDialog: false,
      // 表单
      roleForm: {
        name: "",
        description: "",
      },
      isEdit: false,
      // 校验规则
      rules: {
        name: [
          { required: true, message: "角色名称不能为空", trigger: "blur" },
        ],
        description: [
          { required: true, message: "角色描述不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    // 初始 调用
    this.loadRoles();
  },
  methods: {
    hAssign(id) {
      this.roleId = id;
      this.showPerDialog = true;
    },
    // 弹出关闭重置表单
    resetForm() {
      this.$refs.roleForm.resetFields();
    },
    // 编辑状态
    hEdit(id) {
      this.showDialog = true;
      this.isEdit = true;
      this.loadRoleDetail(id);
    },
    // 添加
    hAdd() {
      this.showDialog = true;
      this.isEdit = false;
    },
    // 编辑 填充
    async loadRoleDetail(id) {
      try {
        const res = await getRolesDetail(id);
        if (res.code !== 10000) return;
        this.roleForm = res.data;
      } catch (error) {
        console.log(error);
      }
    },
    // 编辑 函数
    async doEdit() {
      try {
        const res = await updateRoles(this.roleForm);
        if (res.code !== 10000) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.showDialog = false;
        this.loadRoles();
      } catch (error) {
        console.log(error);
      }
    },
    // 兜底 编辑&添加
    hSubmit() {
      this.$refs.roleForm.validate((valid) => {
        if (!valid) return;
        this.isEdit ? this.doEdit() : this.doAdd();
      });
    },
    // 添加函数
    async doAdd() {
      try {
        const res = await addRoles(this.roleForm);
        if (res.code !== 10000) return this.$message.error(res.message);
        this.$message.success(res.message);
        this.showDialog = false;
        // 最大数据加一
        this.total++;
        // 最大页数
        const MaxPage = Math.ceil(this.total / this.pageParams.pagesize);
        // 跳转最大页数
        this.pageParams.page = MaxPage;
        this.loadRoles();
      } catch (error) {
        console.log(error);
      }
    },
    // 删除 按钮 & 函数
    hDel(id) {
      this.$confirm("您确定删除该角色吗?")
        .then(async () => {
          const res = await delRoles(id);
          if (res.code !== 10000) return this.$message.error(res.message);
          this.$message.success(res.message);
          this.loadRoles();
        })
        .catch((error) => console.log(error));
    },
    // 多少条
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageParams.page = 1;
      this.pageParams.pagesize = val;
      this.loadRoles();
    },
    // 每一页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageParams.page = val;
      this.loadRoles();
    },
    // 渲染列表
    async loadRoles() {
      try {
        const res = await getRoles(this.pageParams);
        console.log(res);
        this.roles = res.data.rows;
        this.total = res.data.total;
      } catch (err) {
        console.log(err);
      }
    },
  },
};
</script>